<?php
/**
 * User: @lixu
 * Date: 2019-05-29
 * Time: 14:59
 */

namespace Biz\service\wiki;

use Biz\service\esSearch\Wiki as esWiki;
use Doraemon\model\Elasticsearch\EsWikiListModel;
use Doraemon\tools\Log;

class ShopGoodsList
{
    const TAB_TYPE = 'shop_goods_list';

    public static function getList($shopId, $page, $sortType, $uid)
    {

        $accountInfo = \Biz\service\brandAccount\BrandAccount::getAllByUid($shopId);

        // 账户和shop
        if (!$accountInfo) {
            Log::debug('不存在的商家#shopId：' . $shopId);
            return ['list' => [], 'total' => 0, 'is_over' => 1];
        }

        $shopUid = 0;
        $accountInfo = current($accountInfo);
        $accountShopList = \Biz\service\brandAccount\BrandAccount::getShopInfos($accountInfo['id']);
        if ($accountInfo['type'] == 1) { // 品牌
            if ($accountShopList) {
                $shopUid = $shopId;
            } else {
                return ['list' => [], 'total' => 0, 'is_over' => 1];
            }
        } else if ($accountInfo['type'] == 2) { // 店铺
            $shopUid = $shopId;
        }


        $where = [
            'shop_uid' => $shopUid
        ];

//        $result = esWiki::getList($where, esWiki::SCENES_APP_SHOP_WIKI_LIST, $sortType, $page);
        $result = EsWikiListModel::getList($where, EsWikiListModel::SCENES_APP_SHOP_WIKI_LIST, $sortType, $page);

        $list = Wiki::getCompleteWikiList($result['rows'], $uid);

        return ['list' => array_values($list), 'total' => $result['total'], 'is_over' => $result['is_over']];
    }
}